package example;

import java.io.FileReader;
import java.io.Reader;

public class Strcount {
	public static int countWords(String file, String find) throws Exception {
	      int count = 0;
	      Reader in = new FileReader(file);
	       int c;
	      while ((c = in.read()) != -1) {
	        while (c == find.charAt(0)) {
	        for (int i = 1; i < find.length(); i++) {
	         c = in.read();
	        if (c != find.charAt(i)) break;
	        if (i == find.length() - 1) count++;
	        }
	       }
	      }
	      return count;
	      }
	public static String subString(String str, int subBytes) {
		int bytes = 0; // 用来存储字符串的总字节数
		for (int i = 0; i < str.length(); i++) {
		if (bytes == subBytes) {
		return str.substring(0, i);
		}
		char c = str.charAt(i);
		if (c < 256) {
		bytes += 1; // 英文字符的字节数看作1
		} else {
		bytes += 2; // 中文字符的字节数看作2
		if(bytes - subBytes == 1){
		return str.substring(0, i);
		}
		}
		}
		return str;
		} 
	
	public static void main(String[] args){
		
		try {
			//System.out.println(countWords("K:/works/MyEclipse 8.5/Test/src/example/count.txt","A"));
			System.out.println(subString("我ABC",4));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
